package com.today.step.lib;

import android.content.Context;
import android.database.Cursor;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ConflictAlgorithm;
import com.today.step.lib.model.TodayStepData;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用来记录当天步数列表，传感器回调30次记录一条数据
 * Created by shangsong on 2017/10/20.
 */

public class TodayStepDBHelper {
    private static final String TAG = "TodayStepDBHelper";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "TodayStepDB.db";
    public static final String TABLE_NAME = "TodayStepData";
    //日期，格式：yyyy-MM-dd
    public static final String COLUMN_DATE_YMD = "date_ymd";
    //日期，格式：毫秒
    public static final String COLUMN_DATE_DS = "date_ms";
    public static final String COLUMN_STEPS = "steps";


    private static LiteOrm liteOrm;

    public synchronized static void createDb(Context context) {
        if (null == liteOrm) {
            DataBaseConfig dataBaseConfig = new DataBaseConfig(context, DATABASE_NAME);
            dataBaseConfig.debugged = true;
            dataBaseConfig.dbVersion = DATABASE_VERSION;
            dataBaseConfig.onUpdateListener = null;
            liteOrm = LiteOrm.newSingleInstance(dataBaseConfig);
        }
    }

    public synchronized static TodayStepData queryOneDaySteps(String date) {
        if (null == liteOrm) {
            return new TodayStepData();
        }
        QueryBuilder<TodayStepData> queryBuilder = new QueryBuilder<>(TodayStepData.class);
        queryBuilder.whereEquals(COLUMN_DATE_YMD, date);
        List<TodayStepData> stepList = liteOrm.query(queryBuilder);
        if (null == stepList || stepList.isEmpty()) {
            return new TodayStepData();
        } else {
            return stepList.get(0);
        }
    }

    public synchronized static List<TodayStepData> queryLastDaysSteps(int limit) {
        if (null == liteOrm) {
            return new ArrayList<>();
        }
        QueryBuilder<TodayStepData> queryBuilder = new QueryBuilder<>(TodayStepData.class);
        queryBuilder.orderBy(COLUMN_DATE_YMD)
                .appendOrderDescBy(COLUMN_DATE_YMD)
                .limit(String.valueOf(limit));
        List<TodayStepData> stepList = liteOrm.query(queryBuilder);
        if (null == stepList || stepList.isEmpty()) {
            return new ArrayList<>();
        } else {
            return stepList;
        }
    }

    public synchronized static LiteOrm getLiteOrm() {
        return liteOrm;
    }

    /**
     * 插入一条记录
     *
     * @param t
     */
    public synchronized static <T> void insert(T t) {
        if (null == liteOrm) {
            return;
        }
        liteOrm.save(t);
    }

    /**
     * 查询  某字段 等于 Value的值
     *
     * @param cla
     * @param field
     * @param value
     * @return
     */
    public synchronized static <T> List<T> getQueryByWhere(Class<T> cla, String field, String value) {
        if (null == liteOrm) {
            return new ArrayList<>();
        }
        QueryBuilder<T> queryBuilder = new QueryBuilder<>(cla);
        queryBuilder.whereEquals(field, value);
        return liteOrm.query(queryBuilder);
    }

    /**
     * 仅在以存在时更新
     *
     * @param t
     */
    public synchronized static <T> void update(T t) {
        if (null == liteOrm) {
            return;
        }
        liteOrm.update(t, ConflictAlgorithm.Replace);
    }

    public synchronized static <T> void deleteByWhere(Class<T> cla, String field, String value) {
        if (null == liteOrm) {
            return;
        }
        WhereBuilder whereBuilder = new WhereBuilder(cla);
        liteOrm.delete(whereBuilder.equals(field, value));
    }
}
